#!/usr/bin/env python
# coding: utf-8

# In[1]:


import os
import numpy as np
import pandas as pd
import scipy
from subprocess import Popen, PIPE
import getpass
import emcee

from jm.library.likelihood import Likelihood, Constraint


# In[2]:


def clean_chain(columns=Likelihood.PARAM_NAMES):
    chain_array = backend.get_chain(flat=False)
    chain_array = chain_array[:, :, :]
    flat_len = int(
        128 * (chain_array.shape[0]-ndiscard) / nthin)
    chain_array = chain_array[
        ndiscard::nthin,:,:]

    chain_array_flat = chain_array.reshape(
        flat_len, chain_array.shape[2])
    df = pd.DataFrame(chain_array_flat, columns=columns)
    return df


# In[3]:


ndiscard = 3000
nthin = 1


# # Baseline

# In[4]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_G1actioninteraction.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES)
df.describe()

with open("figs/table7.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)


# # Additional covariate - share repaid by 3 and age

# In[11]:


PARAM_NAMES_multicov = Likelihood.PARAM_NAMES[0:8] + (
    "prob_repayment_endo_covariates", "share_repaid_by_3", 'age') + Likelihood.PARAM_NAMES[9:]


# In[12]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_multicov_shareby3_age.h5')
state = backend.get_last_sample()
df = clean_chain(columns=PARAM_NAMES_multicov)
df.describe()

with open("figs/tableOB5.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)


PARAM_NAMES_multicov_missing = Likelihood.PARAM_NAMES[0:8] + (
    "prob_repayment_endo_covariates", "share_repaid_by_3", 'age',
    'is_age_missing', 'is_share_repaid_by_3_missing') + Likelihood.PARAM_NAMES[9:]


# In[12]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_multicov_shareby3_age_missing.h5')
state = backend.get_last_sample()
df = clean_chain(columns=PARAM_NAMES_multicov_missing)
df.describe()

with open("figs/tableOB5_missing.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)


# # Additional covariate - cumulative calls

# In[15]:


PARAM_NAMES_calls = Likelihood.PARAM_NAMES[0:9] + ('total_calls', ) + Likelihood.PARAM_NAMES[9:]


# In[16]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_with_calls_data.h5')
state = backend.get_last_sample()
df = clean_chain(columns=PARAM_NAMES_calls)
df.describe()


with open("figs/tableOB6.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)
    print("", file=text_file)
    print("Note: multiply calls coefficient by 60 to get coefficient in table ob6", file=text_file)

# # Pre/post June interaction

# In[23]:


PARAM_NAMES_prepostjune = Likelihood.PARAM_NAMES[0:9] + (
                "post_june_G1", "post_june_writ", "post_june_G1_writ"
            ) + Likelihood.PARAM_NAMES[9:]


# In[24]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_pre_post_june.h5')
state = backend.get_last_sample()
df = clean_chain(columns=PARAM_NAMES_prepostjune)
df.describe()


# In[25]:


with open("figs/tableOB8_columns12.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)



# # Pre/post July interaction

# In[27]:


PARAM_NAMES_prepostjuly = Likelihood.PARAM_NAMES[0:9] + (
                "post_july_G1", "post_july_rec1", "post_july_G1_rec1",
            ) + Likelihood.PARAM_NAMES[9:]

# In[29]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_pre_post_july.h5')
state = backend.get_last_sample()
df = clean_chain(columns=PARAM_NAMES_prepostjuly)
df.describe()


# In[30]:

with open("figs/tableOB8_columns34.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)




# # Time Trend

# In[32]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_timetrend.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES_TT)
df.describe()

with open("figs/tableOB4.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)



# # Valor Regime

# In[35]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_valorregime.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES_VR)
df.describe()


with open("figs/tableOB12.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)



# # No Valor Bound

# In[38]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_relaxedbound.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES)
df.describe()


with open("figs/tableOB11.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)



# # Q1

# In[41]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_Q1.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES)
df.describe()

with open("figs/tableOB3.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)


# # Voluntary

# In[44]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_voluntary.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES)
df.describe()

with open("figs/tableOA2.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)


# # Interacting G1 Deadline with Rec1, Medida

# In[47]:


PARAM_NAMES = Likelihood.PARAM_NAMES[0:9] + (
                "above_med_G1_deadline", "above_med_G1_deadline_rec1", "above_med_G1_deadline_medida"
            ) + Likelihood.PARAM_NAMES[9:]


# In[48]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_G1deadlineinteraction.h5')
state = backend.get_last_sample()
df = clean_chain(columns=PARAM_NAMES)
df.describe()


# In[49]:

with open("figs/tableOB7.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)


# # Logistic

# In[58]:


backend = emcee.backends.HDFBackend('estimation/parameter_estimates/jesus_maria_mcmc_logistic.h5')
state = backend.get_last_sample()
df = clean_chain(columns=Likelihood.PARAM_NAMES)
df.describe()


# In[59]:


with open("figs/tableOB9.txt", "w") as text_file:
    print("Mean",file=text_file)
    print(df.mean(), file=text_file)
    print("Std", file=text_file)
    print(df.std(), file=text_file)




